Typical Usage:
Required bean name is "AM1".
There are the following three typical usage modes:
(1)
The simplest usage of the bean is a communication without interrupts (polling mode).
MAIN.C
byte ch;
byte err;
AM1_TError error;
void main(void)
{
AM1_SelectSlave(1); //Select slave with number 1
for(;;) {
//Wait for a character
while(AM1_GetCharsInRxBuf() == 0);
//Read received character and send it if no error is detected
if( AM1_RecvChar(&ch) == ERR_OK) {
AM1_SendChar(ch);
} else {
// The GetBreak method must be used first because
// the GetError method clear the break flag
// This method is available if the Break
// signal is enabled (Advanced view).
AM1_GetBreak(&b);
if (b) {
// the break character was received
}
AM1_GetError(&error)
if(error.errName.Parity)
//Parity error
else if(error.errName.Framing)
//Framing error
.
.
.
}
}
}
(2)
The next typical usage of this bean is a communication with interrupts using communication events.
EVENTS.C
void AM1_OnRxChar(void)
{
byte ch;
//Read received character and if no error is detected
if(AM1_RecvChar(&ch) == ERR_OK)
//send it back
AM1_SendChar(ch);
}
(3)
Following example shows how to recognize communication error using the GetError method.
In the interrupt communication mode the user can use the OnError event to recognize an error during
the communication.
MAIN.C
byte ch;
AM1_TError error; //TError union is defined in the header file
void main(void)
{
AM1_SelectSlave(1); //Select slave with number 1
//Wait for a character
while(AM1_GetCharsInRxBuf() == 0);
//Read received character
AM1_RecvChar(&ch);
//Read the last communication errors
AM1_GetError(&error)
if(error.err == 0) {
//Send the last received character if no error is detected
AM1_SendChar(ch);
} else {
if(error.errName.Parity) {
//Parity error
} else if(error.errName.Framing) {
//Framing error
}
.
.
}
.
.
}
For more about typical usage of the bean code please refer to the page Bean Code Typical Usage.
|